library(tidyr)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.1     ✔ purrr     1.0.1
## ✔ forcats   1.0.0     ✔ readr     2.1.4
## ✔ ggplot2   3.4.2     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
setwd('C:/Users/cy_su/PycharmProjects/DSCI_605_Data_Visualizations/Module 5/M5_Lab4/')

data <- read.csv("Fouryears_all.csv")

data$Date <- ymd_hms(data$Date)

start_date <- as.Date("2019-09-01")
end_date <- as.Date("2021-09-01")
filtered_data <- data %>%
  filter(Date >= start_date & Date <= end_date & Primary.Type == "BATTERY")

data_hourly <- filtered_data %>%
  mutate(Hour = floor_date(Date, "hour")) %>%
  group_by(Hour) %>%
  summarise(Count = n()) %>%
  ungroup()

plot <- ggplot(data = data_hourly, aes(x = Hour, y = Count)) +
  geom_line(aes(color = "Count"), size = 1.2, linetype = "solid") +
  geom_point(size = 2, color = "blue") +
  scale_x_datetime(date_labels = "%Y-%m-%d", date_breaks = "1 day") +
  labs(x = "Time",
       y = "Count",
       title = "Two-Year Time Series Visualization",
       caption = "Source: Your Data Source") +
  theme_minimal() +
  theme(axis.text = element_text(size = 10),
        axis.title = element_text(size = 12),
        plot.title = element_text(size = 16, face = "bold", hjust = 0.5),
        plot.caption = element_text(size = 8),
        legend.position = "bottom",
        panel.grid.major = element_line(color = "gray", size = 0.1),
        panel.grid.minor = element_line(color = "gray", size = 0.05))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
plotly_plot <- ggplotly(plot)

plotly_plot

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.